package sanshi.contect.observer.v6;

import java.util.ArrayList;
import java.util.List;

/**
 * 将事件抽取成为一个类，被观察者一个类，观察者一个类。
 * 将这个事件作为参数传递到观察者中，让观察者进行处理
 * 事件原对象就是被观察者，也就是事件产生对象
 */
public class Main {
    public static void main(String[] args) {
        Child child = new Child();
        child.wakeUp();
    }
}

/**
 * 事件类
 */
class WakeUpEvent{
    long timeTamp;
    String loc;

    public WakeUpEvent(long timeTamp, String loc){
        this.timeTamp = timeTamp;
        this.loc = loc;
    }

    @Override
    public String toString() {
        return "WakeUpEvent{" +
                "timeTamp=" + timeTamp +
                ", loc='" + loc + '\'' +
                '}';
    }
}

interface Observer{
    void actionOnWakeUp(WakeUpEvent event);
}

/**
 * 观察者
 */
class Dad implements Observer{

    @Override
    public void actionOnWakeUp(WakeUpEvent event) {
        System.out.println("Dad-----》" + event);
        System.out.println("dad feeding ......");
    }
}
class Mum implements Observer{

    @Override
    public void actionOnWakeUp(WakeUpEvent event) {
        System.out.println("Mum-----》" + event);
        System.out.println("mun hugging  ......");
    }
}
class Dog implements Observer{

    @Override
    public void actionOnWakeUp(WakeUpEvent event) {
        System.out.println("Dog-----》" + event);
        System.out.println("wang wang wang ......");
    }
}

/**
 * 被观者
 */
class Child{

    boolean cry;
    List<Observer> observers = new ArrayList<Observer>();

    {
        observers.add(new Dad());
        observers.add(new Mum());
        observers.add(new Dog());
    }

   public boolean isCry(){
        return cry;
   }
   public void wakeUp(){
        cry = true;
        WakeUpEvent event = new WakeUpEvent(System.currentTimeMillis(), "bad");
        for (Observer observer : observers){
            observer.actionOnWakeUp(event);
        }
   }
}


